package chatqueue;

import java.util.LinkedList;
import java.util.Queue;
import javax.ejb.Stateful;
import javax.jms.Destination;

@Stateful
public class ChatQueueEJB implements ChatQueueEJBRemote {

    //Stack queue = new Stack();
    LinkedList queue = new LinkedList();

    public void addClientToQueue(Destination clientDestination) {
        queue.offer(clientDestination);
        System.out.println(": client added");
    }

    public Destination getNextInQueue() {
        System.out.println(queue.size());
        if(!queue.isEmpty()) {
                Destination next = (Destination)queue.poll();
                System.out.println(": tatt ut av køa");
                return next;
        }
        else return null;
    }


    // Look at next client in queue.
    public Destination peek() {
        if(!queue.isEmpty()) {
                Destination next = (Destination)queue.peek();
                return next;
        }
        else return null;
    }

    public int getQueueNumber(Destination destination) {
        for(Object dest: queue)
            if(dest.toString().equalsIgnoreCase(destination.toString()))
                return queue.indexOf(dest);
        return -1;
    }
}
